package cn.isjinhao.crws.sql.lexical.token;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author ISJINHAO
 * @Date 2021/3/1 22:24
 */
public class TokenTypeLexemeMapping {

    private static Map<String, TokenType> lexemeToTokenType = new HashMap<>();
    private static Map<TokenType, String> tokenTypeToLexeme = new HashMap<>();

    public static final String PLUS = "+";
    public static final String MINUS = "-";
    public static final String STAR = "*";
    public static final String SLASH = "/";

    public static final String LEFT_PAREN = "(";
    public static final String RIGHT_PAREN = ")";

    public static final String COMMA = ",";

    public static final String BANG = "!";
    public static final String BANG_EQUAL = "!=";
    public static final String EQUAL = "=";
    public static final String GREATER = ">";
    public static final String GREATER_EQUAL = ">=";
    public static final String LESS = "<";
    public static final String LESS_EQUAL = "<=";

    static {
        lexemeToTokenType.put(PLUS, TokenType.PLUS);
        lexemeToTokenType.put(MINUS, TokenType.MINUS);
        lexemeToTokenType.put(STAR, TokenType.STAR);
        lexemeToTokenType.put(SLASH, TokenType.SLASH);
        lexemeToTokenType.put(LEFT_PAREN, TokenType.LEFT_PAREN);
        lexemeToTokenType.put(RIGHT_PAREN, TokenType.RIGHT_PAREN);
        lexemeToTokenType.put(COMMA, TokenType.COMMA);
        lexemeToTokenType.put(BANG, TokenType.BANG);
        lexemeToTokenType.put(BANG_EQUAL, TokenType.BANG_EQUAL);
        lexemeToTokenType.put(EQUAL, TokenType.EQUAL);
        lexemeToTokenType.put(GREATER, TokenType.GREATER);
        lexemeToTokenType.put(GREATER_EQUAL, TokenType.GREATER_EQUAL);
        lexemeToTokenType.put(LESS, TokenType.LESS);
        lexemeToTokenType.put(LESS_EQUAL, TokenType.LESS_EQUAL);

        tokenTypeToLexeme.put(TokenType.PLUS, PLUS);
        tokenTypeToLexeme.put(TokenType.MINUS, MINUS);
        tokenTypeToLexeme.put(TokenType.STAR, STAR);
        tokenTypeToLexeme.put(TokenType.SLASH, SLASH);
        tokenTypeToLexeme.put(TokenType.LEFT_PAREN, LEFT_PAREN);
        tokenTypeToLexeme.put(TokenType.RIGHT_PAREN, RIGHT_PAREN);
        tokenTypeToLexeme.put(TokenType.COMMA, COMMA);
        tokenTypeToLexeme.put(TokenType.BANG, BANG);
        tokenTypeToLexeme.put(TokenType.BANG_EQUAL, BANG_EQUAL);
        tokenTypeToLexeme.put(TokenType.EQUAL, EQUAL);
        tokenTypeToLexeme.put(TokenType.GREATER, GREATER);
        tokenTypeToLexeme.put(TokenType.GREATER_EQUAL, GREATER_EQUAL);
        tokenTypeToLexeme.put(TokenType.LESS, LESS);
        tokenTypeToLexeme.put(TokenType.LESS_EQUAL, LESS_EQUAL);
    }

    public static String getLexeme(TokenType tokenType) {
        return tokenType == null ? null : tokenTypeToLexeme.get(tokenType);
    }

    public static TokenType getTokenType(String lexeme) {
        return lexeme == null ? null : lexemeToTokenType.get(lexeme);
    }

}
